﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AvisoSuporte.Model;

namespace AvisoSuporte
{
    public partial class ProcessarRelatorioRemedy : Form
    {
        

        

        List<GridRemedy> listaRemedy = new List<GridRemedy>();

        public ProcessarRelatorioRemedy()
        {
            InitializeComponent();
        }

        private void ProcessarRelatorioRemedy_Load(object sender, EventArgs e)
        {
            //load do relatorio, pegar informações do remedy e também do TFS

            List<int> numerosIncidentes = new List<int>();

            Remedy remedy = new Remedy();
            AvisoSuporte.remedyWS.GetListOutputMapGetListValues[] listaRemedyGet = remedy.getIncidentsWS(@" ('Assigned Group' = ""DO-CDSB-SUPORTE-CODIFICACAO"" or 'Assigned Group' = ""DO-DSE-SUPORTE NIVEL 3"")");


            foreach (AvisoSuporte.remedyWS.GetListOutputMapGetListValues item in listaRemedyGet)
            {
                if (item.Status != AvisoSuporte.remedyWS.StatusType.Closed
                    && item.Status != AvisoSuporte.remedyWS.StatusType.Cancelled
                    && item.Status != AvisoSuporte.remedyWS.StatusType.Resolved)
                {

                    //verifica os workitens do tfs
                    numerosIncidentes.Add(ExtraiNumero(item.Incident_Number));
                }
            }

            Communication.Geral geral = new AvisoSuporte.Communication.Geral();
            List<Model.WorkItem> workItens = geral.RetornaWorkItensPorRemedy(numerosIncidentes);


            foreach (AvisoSuporte.remedyWS.GetListOutputMapGetListValues item in listaRemedyGet)
            {
                if (item.Status != AvisoSuporte.remedyWS.StatusType.Closed
                    && item.Status != AvisoSuporte.remedyWS.StatusType.Cancelled
                    && item.Status != AvisoSuporte.remedyWS.StatusType.Resolved)
                {
                    GridRemedy linha = new GridRemedy();
                    linha.Id = ExtraiNumero(item.Incident_Number);
                    linha.Titulo = item.Description;
                    linha.ListaWorkItens = RetornaLista(workItens, linha.Id);
                    listaRemedy.Add(linha);
                }

            }

            dtgRemedy.DataSource = listaRemedy;

            


            
        }

        private List<GridWorkItems> RetornaLista(List<AvisoSuporte.Model.WorkItem> workItens, int numeroIncidente)
        {
            List<GridWorkItems> retorno = new List<GridWorkItems>();
            foreach (Model.WorkItem item in workItens) 
            {
                if(item.Remedy != string.Empty)
                    if (Convert.ToInt32(item.Remedy) == numeroIncidente)
                        retorno.Add(new GridWorkItems() { Id = item.ID, DataFinal = item.FinishDate, DataInicial = item.StartDate, Status = item.State, Titulo = item.Title, Tipo = item.WorkItemType , CreatedBy = item.CreatedBy});

            }
            return retorno;
        }

        private int ExtraiNumero(string incidente)
        {
            return Convert.ToInt32(incidente.Replace("INC",""));
        }

        private void dtgRemedy_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgRemedy.SelectedRows.Count >0) 
            {
                dtgWorkItens.DataSource = null;
                dtgWorkItens.DataSource = ((Model.GridRemedy)dtgRemedy.SelectedRows[0].DataBoundItem).ListaWorkItens;
            }
        }

        private void dtgRemedy_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in dtgRemedy.Rows)
            {
                if (((Model.GridRemedy)(row.DataBoundItem)).ListaWorkItens.Count == 0)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        cell.Style.BackColor = Color.NavajoWhite;
                    }
                }
                else if (((Model.GridRemedy)(row.DataBoundItem)).ListaWorkItens.Count > 0 && VerificaWorkItens(((Model.GridRemedy)(row.DataBoundItem)).ListaWorkItens))
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        cell.Style.BackColor = Color.PowderBlue;
                    }
                }
                else
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        cell.Style.BackColor = Color.YellowGreen;
                    }
                }

            }
        }

        private bool VerificaWorkItens(List<GridWorkItems> list)
        {
            foreach (GridWorkItems item in list) 
            {
                if (item.Status == "Analise" ||
                item.Status == "Codificacao" ||
                item.Status == "Codificado" ||
                item.Status == "Ativo" ||
                item.Status == "Ativa") 
                {
                    return true;
                }
            }

            return false;
        }


    }
}
